package com.jbl.te.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jbl.commonutils.R;
import com.jbl.te.entity.College;
import com.jbl.te.entity.Department;
import com.jbl.te.entity.Major;
import com.jbl.te.entity.vo.MajorQuery;
import com.jbl.te.entity.vo.MajorVO;
import com.jbl.te.service.CollegeService;
import com.jbl.te.service.DepartmentService;
import com.jbl.te.service.MajorService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jbl
 * @since 2020-12-11
 */
@RestController
@RequestMapping("/te/major")
@CrossOrigin
public class MajorController {

    @Autowired
    private MajorService majorService;
    @Autowired
    private CollegeService collegeService;
    @Autowired
    private DepartmentService departmentService;

    @GetMapping("/getAllMajor")
    public R getAllMajor() {
        return R.ok().data("list",majorService.list());
    }

    @GetMapping("/getAllMajorByDepartmentId")
    public R getAllMajorByDepartmentId(String id) {
        QueryWrapper<Major> wrapper = new QueryWrapper<>();
        wrapper.eq("department_id",id);
        return R.ok().data("list",majorService.list(wrapper));
    }

    @PostMapping("{page}/{limit}")
    @ApiOperation("分页系列表")
    public R pageList(@PathVariable("page") Long page, @PathVariable("limit") Long limit, @RequestBody(required = false) MajorQuery majorQuery) {
        Page<Major> pageParam = new Page<>(page, limit);
        majorService.pageQuery(pageParam,majorQuery);
        List<Major> records = pageParam.getRecords();
        List<MajorVO> list = new ArrayList<>();
        for (Major major : records) {
            MajorVO vo = new MajorVO();
            BeanUtils.copyProperties(major,vo);
            Department department = departmentService.getById(major.getDepartmentId());
            vo.setDepartmentName(department.getName());
            College college=collegeService.getById(department.getCollegeId());
            vo.setCollegeId(department.getCollegeId());
            vo.setCollegeName(college.getName());
            list.add(vo);
        }
        long total = pageParam.getTotal();
        return R.ok().data("total", total).data("list", list);
    }
    @PostMapping("saveMajor")
    @ApiOperation("添加专业(TODO)")
    public R saveMajor(@RequestBody Major major) {

        boolean b = majorService.save(major);
        if(b) {
            return R.ok();
        } else {
            return R.error().message("添加失败");
        }
    }
    @PutMapping("updateMajor")
    @ApiOperation("修改专业")
    public R updateDepartment(@RequestBody Major major) {
        boolean b = majorService.saveOrUpdate(major);
        if(b) {
            return R.ok();
        } else {
            return R.error().message("修改失败");
        }
    }
    @GetMapping("getMajorById")
    @ApiOperation("根据id查找专业")
    public R getMajorById(String id) {
        Major major = majorService.getById(id);
        if(major == null) {
            return R.error().message("系不存在");
        }
        MajorVO vo = new MajorVO();
        BeanUtils.copyProperties(major,vo);
        Department department = departmentService.getById(major.getDepartmentId());
        vo.setDepartmentName(department.getName());
        College college=collegeService.getById(department.getCollegeId());
        vo.setCollegeId(department.getCollegeId());
        vo.setCollegeName(college.getName());
        return R.ok().data("major",vo);
    }
    @DeleteMapping("delMajorById")
    @ApiOperation("删除专业")
    public R delMajorById(String id) {
        boolean b = majorService.removeById(id);
        if(b) {
            //删除功能 TODO
            return R.error().message("暂不支持删除功能");
//            return R.ok();
        } else {
            return R.error().message("删除失败");
        }
    }
    @DeleteMapping("delMajorByList")
    @ApiOperation("批量删除专业")
    public R delMajorsByList(@RequestBody List<String> idList) {
            //删除功能 TODO
            return R.error().message("暂不支持删除功能");
//        boolean b = majorService.removeByIds(idList);
//        if(b) {
////            return R.ok();
//        }
//        return R.error().message("批量删除失败");
    }



}

